#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Last Update:

'''docstring
'''

__revision__ = '0.1'
__author__ = 'lxd'

import sys
sys.path.append('.')
sys.path.append('..')
from PIL import ImageGrab
import time
import os
import wx
import win32con
import bin_analyseImg

class MainFrame(wx.Frame):
    def __init__(self,log,path):
        wx.Frame.__init__(self, None, -1, u'截屏小程序', size=(600, 400))
        self.log = log
        self.path = path
        self.regHotKey()
        self.Bind(wx.EVT_HOTKEY, self.OnHotKeyFetchWithDialog, id=self.hotKeyId_start)
        self.Bind(wx.EVT_HOTKEY, self.OnHotKeyEnd, id=self.hotKeyId_end)
        self.Bind(wx.EVT_HOTKEY, self.OnHotKeyQuit, id=self.hotKeyId_quit)
        self.Bind(wx.EVT_HOTKEY, self.OnHotKeyFetch, id=self.hotKeyId_fetch)
        self.log.info(u'使用方法：')
        self.log.info(u'Alt+F1:截屏')
        self.log.info(u'Alt+F2:开始分析')
        self.log.info(u'Alt+F3:关闭')
        
    def regHotKey(self):
        self.hotKeyId_start = 100
        self.RegisterHotKey(self.hotKeyId_start, win32con.MOD_ALT, win32con.VK_F1)
        self.hotKeyId_end = 101
        self.RegisterHotKey(self.hotKeyId_end, win32con.MOD_ALT, win32con.VK_F2)
        self.hotKeyId_quit = 102
        self.RegisterHotKey(self.hotKeyId_quit, win32con.MOD_ALT, win32con.VK_F3)
        self.hotKeyId_fetch= 103
        self.RegisterHotKey(self.hotKeyId_fetch, win32con.MOD_ALT, win32con.VK_F4)

    def __capture(self,filename):
        pathname = r'%s/%s.bmp' %(self.path,filename)
        im = ImageGrab.grab()
        im.save(pathname)

    def __getFilename(self):
        if not os.path.isdir(self.path) or not os.path.exists(self.path):
            os.makedirs(self.path)
        filename = time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))
        return filename
        
    def OnHotKeyFetchWithDialog(self, evt):
        filename = self.__getFilename()
        dialog = wx.TextEntryDialog(None,'enter file name','file name',filename,style=wx.OK|wx.CANCEL)
        if dialog.ShowModal() == wx.ID_OK:
            filename = dialog.GetValue()
        dialog.Destroy()
        self.__capture(filename)
            
    def OnHotKeyFetch(self, evt):
        filename = self.__getFilename()
        self.__capture(filename)

    def OnHotKeyEnd(self, evt):
        app = wx.App(redirect = False)
        bin_analyseImg.MainFrame(self.log,self.path).Show()
        app.MainLoop()

    def OnHotKeyQuit(self, evt):
        exit()

if __name__ == '__main__':
    import config
    log = config.log
    path = r'%s/fetchImg/img' %config.path

    app = wx.App(redirect = False)
    MainFrame(log,path)
    app.MainLoop()
